package ext.vpde.cadvaulting.migration;

import java.io.*;
import java.util.*;

import ext.vpde.cadvaulting.util.WINDSQL;

public class LoadVariants {

	public static void main ( String [] args ) throws Exception {
		if (args.length != 1) {
			System.out.println("\nUsage: java.ext.vpde.cadvaulting.LoadVariants loadfile");
		} else {
			System.out.println("BEGIN LOAD VARIANTS: " + Calendar.getInstance().getTime());
			WINDSQL.openConnection();
			BufferedReader file = new BufferedReader(new FileReader(args[0]));
			String s = file.readLine();
			int lineNumber = -1;
			while (s!= null && s.length() > 0) {	
				StringTokenizer strTok = new StringTokenizer(s,"|");
				String iName = strTok.nextToken();
				String iVer  = strTok.nextToken();
				String iIter = strTok.nextToken();
				String gName = strTok.nextToken();
				
		    	String genIda2a2 = WINDSQL.getIdOfDocumentMaster(gName);
		    	String insIda2a2 = WINDSQL.getIdFromDocumentName(iName,iVer,iIter);
		    	
		    	if (genIda2a2 == null) {
		    		System.out.println("GENERIC NOT FOUND: " + gName);
		    	} else {
		    		if (insIda2a2 == null) {
		    			System.out.println("INSTANCE NOT FOUND: " + iName);
		    		} else {
		    			WINDSQL.createVariantLink(genIda2a2, insIda2a2);
		    		}
		    	}
				s = file.readLine();
				LoadUtil.printProgress(++lineNumber);
			}
			WINDSQL.closeConnection();
			System.out.println("-END- LOAD VARIANTS: " + Calendar.getInstance().getTime());
		}
	}
}